package com.wanwan;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wanwan.tools.PinYin;
import com.wanwan.tools.QRCodeTools;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 测试解析二维码 html
 */
public class TestParseHtml {

    public static void main(String[] args) throws IOException {
        //String str = QRCodeTools.deQrCodeByBaidu("C:\\Users\\admin\\Desktop\\qrcode\\图片13.png");
        String str = QRCodeTools.deQrCodeByBaidu("C:\\Users\\admin\\Desktop\\qrcode\\2.jpg");
        JSONObject jsonObjectStr = JSON.parseObject(str);
        JSONObject jsonObject = new JSONObject();
        List<Map<String, JSONArray>> mapList = (List<Map<String, JSONArray>>) JSON.parse(jsonObjectStr.getString("codes_result"));
        if (mapList.size() == 0 ) {
            System.out.println("二维码解析失败");
        }else {
            for (Map<String, JSONArray> map : mapList) {
                JSONArray jsonArray = map.get("text");
                if (jsonArray.size() ==1){
                    for (Object text : jsonArray) {
                        if(text.toString().contains("http")){
                            String jsonString = parseHtml(text);
                            System.out.println(jsonString);
                        }else if (text.toString().contains("合格证编号")){
                            String[] strings1 = text.toString().split(";");
                            for (String string : strings1) {
                                String[] strings2 = string.split(":");
                                for (int i=0; i<strings2.length; i++){
                                    String pinYin = PinYin.getPinYinHeadChar(strings2[i].trim());
                                    jsonObject.put(pinYin,strings2[i+1].trim());
                                    break;
                                }
                            }
                            System.out.println(jsonObject.toJSONString());
                        }
                    }
                }else {
                    for (Object text : jsonArray){
                        String[] string1 = text.toString().split(":");
                        for (int i=0; i<string1.length; i++){
                            String pinYin = PinYin.getPinYinHeadChar(string1[i].trim());
                            jsonObject.put(pinYin,string1[i+1]);
                            break;
                        }
                    }
                    System.out.println(jsonObject.toJSONString());
                }
            }
        }
    }




    /**
     * 解析合格证html
     * @param text 二维码连接
     * @return
     * @throws IOException
     */
    public static String parseHtml(Object text) throws IOException {
        JSONObject jsonObject = new JSONObject();
        Document doc = Jsoup.connect(text.toString()).get();

        if (doc.select(".wide").size()!=0 && doc.select(".value").size()!=0){
            Elements elementKey = doc.select(".wide");
            List<String> listKey = new ArrayList<>();
            for (Element element : elementKey) {
                String string = element.text();
                String pinYin = PinYin.getPinYinHeadChar(string);
                listKey.add(pinYin);
            }
            Elements elementValue = doc.select(".value");
            List<String> listValue = new ArrayList<>();
            for (Element element : elementValue) {
                String string = element.text();
                listValue.add(string);
            }
            for (int i=0;i<listKey.size();i++){
                for (int x=0; x<listValue.size(); x++){
                    jsonObject.put(listKey.get(i), listValue.get(i));
                    break;
                }
            }
        }else {
            String body = doc.body().text();
            String[] strings = body.split(" ");
            for (String string : strings){
                String[] strings1 = string.split("：");
                for (int i =0;i<strings1.length;i++){
                    if (strings1.length != 1){
                        String pinYin = PinYin.getPinYinHeadChar(strings1[i]);
                        jsonObject.put(pinYin,strings1[i+1]);
                        break;
                    }
                }
            }
        }
        return jsonObject.toJSONString();
    }
}
